package gxnu.edu.cn.controller;


import gxnu.edu.cn.entity.Cuser;
import gxnu.edu.cn.entity.DTO.CuserLoginDTO;
import gxnu.edu.cn.entity.vo.PasswordRestVo;
import gxnu.edu.cn.service.ICuserService;
import com.sky.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author cq
 * @since 2025-05-07
 */


@RestController
@RequestMapping
@Slf4j
public class CuserController {
   @Autowired
        private ICuserService cuserService;


    /**
     * 登录
     * @param
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<Cuser> login(@RequestBody CuserLoginDTO cuserLoginDTO) {
//    log.info("用户登录：{}", cuserLoginDTO);
        Cuser user = cuserService.login(cuserLoginDTO);
        if (user == null) {
            return Result.error("用户名或密码错误");
        }
        Cuser cuserVO = new Cuser();
        cuserVO.setUserID(user.getUserID());
        cuserVO.setUserName(user.getUserName());
        return Result.success(cuserVO);
    }



    /**
         * 忘记口令重置验证
         * @param
         * @return
         */
        @GetMapping("/sendVerificationEmail")
        public Result sendVerificationEmail(@RequestParam String userName) {
            //String userName = payload.get("userName");

            // 调用服务层发送邮件
            boolean success = cuserService.sendVerificationEmail(userName);

            if (!success) {
                return Result.error("用户不存在或邮件发送失败");
            }

            return Result.success("邮箱发送成功，请注意查收！未完成当前默认为654321");
        }

        @PostMapping("/validEmailCode")
        public Result validEmailCode(@RequestBody PasswordRestVo passwordRestVo){
            int result = cuserService.validEmailCode(passwordRestVo.getUserName(), passwordRestVo.getVerificationCode());
            if(result == 0)return Result.error("验证码错误或已过期！");
            return Result.success("验证成功！");

        }


    }


